var actType;//1：回答问题，2：编辑问题
var editId;
var checkFlag = true;
$(function() {
	initTable();
	doSearch();
	$('#addWin').myWindow({
		'width'  : 450,
		'height' : 520,
		'title'  : "更新答案",
		'closed' :true
	});
	$('#showAnswerWin').myWindow({
		'width'  : 450,
		'height' : 520,
		'title'  : "查看答案",
		'closed' :true
	});
	$('#answerQuestionWin').myWindow({
		'width'  : 450,
		'height' : 520,
		'title'  : "回答问题",
		'closed' :true
	});
	$('#cancleAddUserBtn').click(function(){
		$('#addWin').myWindow('close');
	});
	
	//关闭showAnswerWin窗口
	$('#cancleshowAnswerBtn').click(function(){
		$('#showAnswerWin').myWindow('close');
	});
	//关闭#answerQuestionWin窗口
	$('#cancleanswerQuestionBtn').click(function(){
		$('#answerQuestionWin').myWindow('close');
	});
	//修改答案时，调用js方法
	$("#editAnswerSpan").click(answerQuestion);
	$("#answerQuestionSpan").click(answerQuestion);
	
});

function initTable(){
	$('#dataGrig').myTable({
		width : getWidth(0.98),
		rownumber : false,
		pagination : true,
		checkbox : false,
		paginationTop : true,
		url : '',
		pageNumberName:'pageNumber',
		pageSizeName:'pageSize',
		queryParams:{},
		pageList:[10,20,30],
		columns: [
					{field:'content',title:'问题内容',width:$(this).width() * 0.2,sortable:false},
					{field:'userName',title:'提问客户',width:$(this).width() * 0.3,sortable:false},  
					{field:'activityTopic',title:'活动主题',width:$(this).width() * 0.3,sortable:false},   
					{field:'id',title:'操作',width:$(this).width() * 0.3,sortable:false,
						//id为问题id
						formatter:function(val,rec){
							var html ;
							if(rec.answer || rec.answer == 0) {
//								html = "&nbsp;<a href='javascript:showAnswer()'>查看答案</a>&nbsp;<a href='javascript:editAnswer()'>修改</a>";
								html='<a class="btn btn-primary btn-xs" onclick="openAddWin('+rec.id+');" href="javaScript:void(0)">更新答案</a>&nbsp;&nbsp;<a class="btn btn-primary btn-xs" onclick="showAnswerWin('+rec.id+');" href="javaScript:void(0)">查看答案</a>';
							}else {
//								html = "&nbsp;<a href='javascript:addAnswer("+val+")'>回复问题</a>";
								html='<a class="btn btn-primary btn-xs" onclick="answerQuestionWin('+rec.id+');" href="javaScript:void(0)">回复问题</a>&nbsp;&nbsp;';
							}
							return html;
		        	}	
				}
				]
	});
}

function changeFileParam() {
	exeluploader.setParams({"activityId":$("#acId").val()});
}

function openAddWin(id) {
	actType = 2;
	editId = id;
	var ac = getDataById("dataGrig", id);
	$('#userName2').val(ac.userName);
	$("#companyName2").val(ac.content);
	$("#userLevel2").val(ac.answer);
	$('#addUserBtn').html("更新");
	$('#addWin').myWindow('open');
	
	$('#userName2').attr("readOnly","true");
	$('#userName2').attr("disabled","disabled");
	$('#companyName2').attr("readOnly","true");
	$('#companyName2').attr("disabled","disabled");
}

//查看答案
function showAnswerWin(id) {
	actType = 2;
	editId = id;
	var ac = getDataById("dataGrig", id);
	$('#userName').val(ac.userName);
	$("#content").val(ac.content);
	$("#answer").val(ac.answer);
	$('#showAnswerWin').myWindow('open');
	
	$('#userName').attr("readOnly","true");
	$('#userName').attr("disabled","disabled");
	$('#content').attr("readOnly","true");
	$('#content').attr("disabled","disabled");
	$('#answer').attr("readOnly","true");
	$('#answer').attr("disabled","disabled");
}

//回答问题
function answerQuestionWin(id) {
	actType = 1;//新增
	editId = id;
	var ac = getDataById("dataGrig", id);
	$('#userNameA').val(ac.userName);
	$("#contentA").val(ac.content);
	$("#answerA").val(ac.answer);
	$('#answerQuestionWin').myWindow('open');
	
	$('#userNameA').attr("readOnly","true");
	$('#userNameA').attr("disabled","disabled");
	$('#contentA').attr("readOnly","true");
	$('#contentA').attr("disabled","disabled");
}
//查询
function doSearch(){
	var queryParams ={};
	if(!checkParams()){
		return;
	}
	formJson=$("#searchForm").form2json();
	url='../questions/list';
	var opts = $('#dataGrig').myTable('options');
//	opts.pageNumber = 1; //注释掉这行，刷新后为当前页面
	queryParams = $.extend(queryParams,formJson);
	opts.queryParams = queryParams;
	opts.url=url;
	$('#dataGrig').myTable('options').url = url,
	$('#dataGrig').myTable('reload');
}

function emptyEditWin(){
	$('#area2').val('');
	$('#remark2').val('');
	$('#topic2').val('');
}
//添加及修改答案
function answerQuestion(){
	//新增试题
	if(actType==1){
		var parameter={};
		var userName=$.trim($("#userNameA").val());
		var content=$.trim($("#contentA").val());
		var answer=$.trim($("#answerA").val());
		parameter['userName']=userName;
		parameter['content']=content;
		parameter['answer']=answer;
		var url='';
		
		url='../questions/answerQuestion';
		parameter['id']=editId;
		$.post(
			url,
			parameter,
			function(result){
				if(result.info){
					$.myMessager.alert({'title':'提示','msg':' <p><strong>'+result.input+'</strong></p>'});
				}else if(result.result){
					$.myMessager.alert({'title':'提示','msg':' <p><strong>更新成功</strong></p>'});
					 $("#answerQuestionWin").myWindow("close");
					doSearch();
				}
			},
			'json'
		);
	}else if(actType==2){//编辑答案
		
		var parameter={};
		var userName=$.trim($("#userName2").val());
		var content=$.trim($("#companyName2").val());
		var answer=$.trim($("#userLevel2").val());
		parameter['userName']=userName;
		parameter['content']=content;
		parameter['answer']=answer;
		var url='';
		
		url='../questions/answerQuestion';
		parameter['id']=editId;
		$.post(
			url,
			parameter,
			function(result){
				if(result.info){
					$.myMessager.alert({'title':'提示','msg':' <p><strong>'+result.input+'</strong></p>'});
				}else if(result.result){
					$.myMessager.alert({'title':'提示','msg':' <p><strong>更新成功</strong></p>'});
					 $("#addWin").myWindow("close");
					doSearch();
				}
			},
			'json'
		);
	}

}

function checkUser(){
	var userName=$.trim($("#userName2").val());
	var companyName=$.trim($("#companyName2").val());
	var userLevel=$.trim($("#userLevel2").val());
	var remark=$.trim($("#remark2").val());

	if(userName==null||userName==''){
		$.myMessager.alert({'title':'提示','msg':' <p><strong>客户名不能为空！</strong></p>'});
		return false;
	}
	if(companyName==null||companyName==''){
		$.myMessager.alert({'title':'提示','msg':' <p><strong>公司名不能为空！</strong></p>'});
		return false;
	}
	if(userName != null && userName.length>80){
		$.myMessager.alert({'title':'提示','msg':' <p><strong>客户名长度不能大于80！</strong></p>'});
		return false;
	}
	if(companyName != null && companyName.length>80){
		$.myMessager.alert({'title':'提示','msg':' <p><strong>公司名长度不能大于80！</strong></p>'});
		return false;
	}
	if(userLevel != null && userLevel.length>20){
		$.myMessager.alert({'title':'提示','msg':' <p><strong>职位长度不能大于20！</strong></p>'});
		return false;
	}
	if(remark != null && remark.length>80){
		$.myMessager.alert({'title':'提示','msg':' <p><strong>备注长度不能大于80！</strong></p>'});
		return false;
	}
	return true;
}

//去除输入框中的空格
function trimInput(jq){
	jq.val($.trim(jq.val()));
}

//
function deleteUserWin(userId){
	$.myMessager.confirm({'title':'温馨提示','msg':'确定删除客户？'},function(result){
		if(result){
			deleteUser(userId);
		}
	});
}
function doReset(){
	document.getElementById("searchForm").reset();
}

function onChangeDate() {
    if ($("#startTime").val() || $("#endTime").val()) {
        old_startTime = $("#startTime").val();
        old_endTime = $("#endTime").val();
    }
    if ($("#base_1").get(0).selectedIndex != 0) {
        document.getElementById("startTime").value = "";
        document.getElementById("endTime").value = "";
        checkFlag = false;
        document.getElementById("checkDateFlag").value = 1;
    } else {
        document.getElementById("startTime").value = old_startTime;
        document.getElementById("endTime").value = old_endTime;
        checkFlag = true;
        document.getElementById("checkDateFlag").value = 0;
    }
}

function checkParams() {
    if(!checkFlag){
        return true;
    }
	if($("#startTime").val()==''||$("#endTime").val()=='') {
		$.myMessager.alert({'title':'提示','msg':' <p><strong>请填写完整开始、结束时间！</strong></p>'});
		return false;
	}
	if(dateDiff($("#startTime").val(),$("#endTime").val())<0) {
		$.myMessager.alert({'title':'提示','msg':' <p><strong>开始时间不能大于结束时间！</strong></p>'});
		return false;
	}
//	if(dateDiff($("#startTime").val(),$("#endTime").val())>31) {
//		$.myMessager.alert({'title':'提示','msg':' <p><strong>时间间隔不能超过一个月！</strong></p>'});
//		return false;
//	}
	return true;
}